﻿Imports System.Data.SqlClient
Imports DTO
Public Class KhachHangDAO
    Dim dp As New DataProvider
    Function LoadDsKH() As DataTable
        Dim kh As DataTable
        kh = dp.ExecuteQuery("LoadKH", Nothing)
        Return kh
    End Function

    Public Sub ThemKH(ByVal khachhang As KhachHangDTO)
        Dim dt As New DataProvider
        Dim para As SqlParameter() = New SqlParameter(3) {}
        para(0) = New SqlParameter("@TenKH", khachhang.TenKH)
        para(1) = New SqlParameter("@DiaChi", khachhang.DiaChi)
        para(2) = New SqlParameter("@SDT", khachhang.SDT)
        para(3) = New SqlParameter("@MaHD", khachhang.MaHD)
        dt.ExecuteNonQuery("InsertKH", para)

    End Sub

    Function LoadDsKHTheoMA(ByVal makh As Integer) As DataTable
        Dim data As New DataTable
        Dim pare As SqlParameter() = New SqlParameter(0) {}
        pare(0) = New SqlParameter("@makh", makh)
        data = dp.ExecuteQuery("LoadKHTheoID", pare)
        Return data
    End Function

    Function LayTatCa() As Collections.IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList
        Dim dt = cn.ExecuteQuery("LayAllKH")
        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                Dim khachhang As New KhachHangDTO()
                khachhang.MaKH = Integer.Parse(dr.Item("MaKH").ToString())
                khachhang.TenKH = dr.Item("TenKH").ToString()
                khachhang.DiaChi = dr.Item("DiaChi").ToString()
                khachhang.SDT = dr.Item("SDT").ToString()
                khachhang.MaHD = Integer.Parse(dr.Item("MaHD").ToString())
                array.Add(khachhang)
            Next
        End If
        Return array
    End Function

    Sub Xoa(ByVal id As Integer)
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaKH", id)
        cn.ExecuteNonQuery("DeleteKH", para)
    End Sub

    Sub capnhap(ByVal KHdto As KhachHangDTO)
        Dim cn As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(4) {}
        para(0) = New SqlParameter("@MaKH", KHdto.MaKH)
        para(1) = New SqlParameter("@TenKH", KHdto.TenKH)
        para(2) = New SqlParameter("@DiaChi", KHdto.DiaChi)
        para(3) = New SqlParameter("@SDT", KHdto.SDT)
        para(4) = New SqlParameter("@MaHD", KHdto.MaHD)
        cn.ExecuteNonQuery("CapNhapKH", para)
    End Sub

    Function layTatCaTB() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LayAllKHID")
    End Function


    Public Function LAYTHEOID(ByVal id As Integer) As KhachHangDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaKH", id)
        Dim dt = cn.ExecuteQuery("LoadKHTheoID", para)
        Dim khachhang As New KhachHangDTO()
        If dt.Rows.Count <> 0 Then
            khachhang.MaKH = Integer.Parse(dt.Rows(0).Item("MaKH").ToString())
            khachhang.TenKH = dt.Rows(0).Item("TenKH").ToString()
            khachhang.SDT = dt.Rows(0).Item("SDT")
            khachhang.DiaChi = dt.Rows(0).Item("DiaChi").ToString()
            khachhang.MaHD = Integer.Parse(dt.Rows(0).Item("MaHD").ToString())
        End If
        Return khachhang
    End Function

End Class
